package com.ppt.android.base.utils

import android.content.Context
import android.graphics.Typeface
import androidx.core.content.res.ResourcesCompat

/**
 *
 * 通过反射，全局替换默认字体
 * 步骤一：在 App 的主题配置默认字体
 * //...
 *   <item name="android:typeface">serif</item>
 *   //...
 * 步骤二：通过反射修改 Typeface 默认字体
 * @author ZouJiaCai
 * @since 2023/3/28 10:20
 */
object ChangeDefaultFontUtils {
    private const val CUSTOM_FONT = "font/source_sans3_regular.ttf"
    fun changeDefaultFont(ctx: Context) {
        try {
            val typeface = Typeface.createFromAsset(ctx.assets, CUSTOM_FONT)
            val defaultField = Typeface::class.java.getDeclaredField("SANS_SERIF")
            defaultField.isAccessible = true
            defaultField[null] = typeface
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}